﻿using UnityEngine;
using System.Collections;

public class TileObjectDrag : MonoBehaviour {

	Vector3 screenPoint;
	Vector3 offset;
		
	void OnMouseDown()
	{
		LevelGrid.Instance.GetComponent<LevelBuilderGUI>().setElementNotif(this.GetComponent<TileObjectPlane>());
		//Move the background to its position
		Vector3 scanPos = gameObject.transform.position;
	    screenPoint = Camera.main.WorldToScreenPoint(scanPos);
	    offset = scanPos - Camera.main.ScreenToWorldPoint(
	        new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
	}
	
	void OnMouseDrag()
	{
		Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
	    Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
		curPosition.z = transform.position.z;
	    transform.position = curPosition;
	}
}
